package com.ll.beautymall.ui.home.homefragment.view;


import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ll.beautymall.R;
import com.ll.beautymall.adapter.HomeVpAdapter;
import com.ll.beautymall.base.BaseFragment;
import com.ll.beautymall.bean.Floor;
import com.ll.beautymall.bean.NavList;
import com.ll.beautymall.ui.home.homefragment.presenter.NavPresenter;
import com.ll.beautymall.ui.home.indexfragment.view.ContentFragment;
import com.ll.beautymall.utils.ConfigPopupWindow;
import com.ll.beautymall.utils.NetUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * 第二层ViewPager
 */
public class HomeFragment extends BaseFragment implements IHomeView, View.OnClickListener {


    @BindView(R.id.home_toolbar)
    Toolbar hoomToolbar;
    @BindView(R.id.home_nav)
    TabLayout homeNav;
    @BindView(R.id.iv_nav_open)
    ImageView ivNavOpen;
    @BindView(R.id.appBar)
    AppBarLayout appBar;
    @BindView(R.id.home_vp_container)
    ViewPager homeVpContainer;
    @BindView(R.id.ib_empty)
    ImageButton ibEmpty;
    @BindView(R.id.fab_back)
    FloatingActionButton fabBack;
    @BindView(R.id.ll_nav)
    LinearLayout llNav;
    private HomeVpAdapter vpAdapter;
    private NavPresenter presenter;
    private View contentView;

    private ConfigPopupWindow configPopupWindow;


    public HomeFragment() {
    }

    @Override
    protected void initView(ViewGroup container) {
        presenter = new NavPresenter(this);

        vpAdapter = new HomeVpAdapter(getChildFragmentManager());
        homeVpContainer.setAdapter(vpAdapter);
        configHeader(container);
        loadData();
    }

    private void loadData() {
        if (NetUtil.hasInternet(getFActivity())) {
            homeVpContainer.setVisibility(View.VISIBLE);
            ibEmpty.setVisibility(View.GONE);
            presenter.setNavData();
        } else {
            Toast.makeText(getFActivity(), "无网络...", Toast.LENGTH_SHORT).show();
            homeVpContainer.setVisibility(View.GONE);
            ibEmpty.setVisibility(View.VISIBLE);
            ibEmpty.setOnClickListener(this);
        }
    }

    private void configHeader(ViewGroup container) {
        configPopupWindow = new ConfigPopupWindow();
        configPopupWindow.initPopupWindow(getFActivity());

        contentView = LayoutInflater.from(getFActivity()).inflate(R.layout.home_nav_open, container, false);

        ImageView iv_pop_close = (ImageView) contentView.findViewById(R.id.iv_nav_open_pop);
        iv_pop_close.setOnClickListener(this);
        ivNavOpen.setOnClickListener(this);

        homeNav.setupWithViewPager(homeVpContainer);
        homeNav.setTabMode(TabLayout.MODE_SCROLLABLE);
        homeNav.setTabTextColors(Color.parseColor("#252827"), Color.parseColor("#CB0000"));
    }

    @Override
    public int getFLayoutId() {
        return R.layout.fragment_home;
    }

    public void setNavData(NavList list) {
        if (list != null) {
            List<Floor> floor = list.getFloor();
            createVpFragmentAndNav(floor);
        }
    }

    private void createVpFragmentAndNav(List<Floor> floor) {
        List<ContentFragment> fragments = new ArrayList<>();
        final List<String> titles = new ArrayList<>();
        for (int i = 0; i < floor.size(); i++) {
            ContentFragment fragment = ContentFragment.createFragment(floor.get(i));
            fragments.add(fragment);
            titles.add(floor.get(i).getTitle());
        }
        vpAdapter.setData(fragments, floor);

        configPopupWindow.setContentView(contentView, hoomToolbar.getWidth(), 740);
        configPopupWindow.setGvData(titles);
        configPopupWindow.setOnPopGvClickLisenter(homeVpContainer);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_open_pop:
                configPopupWindow.disimssPopup();
                break;
            case R.id.iv_nav_open:
                configPopupWindow.showPopup(hoomToolbar);
                break;
            case R.id.ib_empty:
                loadData();
                break;
            case R.id.fab_back:
                break;
        }
    }

    @Override
    public void showErrorMsg(String msg) {
        Toast.makeText(getFActivity(), msg, Toast.LENGTH_SHORT).show();
    }
}
